<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of user_c
 *
 * @author Костя
 */
class User extends CI_Controller {

    function index() {
        return $this->getuserpage();
    }

    function register() {
        $this->output->enable_profiler(TRUE);
        if ($this->session->userdata('logged_in')) {
            redirect('/', 'location', 301);
        }
        $this->load->helper('form');
        //$data['token'] = md5(rand(10000, 99999));
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Имя пользователя', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Пароль', 'trim|required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Подтверждение пароля', 'trim|required');
        $this->form_validation->set_rules('email', 'Почта', 'trim|required|valid_email');
        $this->form_validation->set_rules('city', 'Город', 'trim|required');
        $this->form_validation->set_rules('phone', 'Телефон', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $data['body'] = $this->load->view('register', '', TRUE);
            $data['title'] = 'Регистрация';
            $this->load->view('main', $data);
        } else {
            $this->load->model('User_model');
            $this->User_model->user_register($this->input->post('username', TRUE), $this->input->post('password', TRUE), $this->input->post('email', TRUE), $this->input->post('city', TRUE), $this->input->post('phone', TRUE));
            //redirect('/', 'location', 301);
        }
    }

    function login() {
        if ($this->session->userdata('logged_in') === TRUE) {
            redirect('/', 'location', 301);
        }

        $this->load->helper('form');
        $token = md5(rand(1000000, 9999999));

        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Имя пользователя', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Пароль', 'trim|required');

        $this->load->model('User_model');
        $result = $this->User_model->make_login($this->input->post('username', TRUE), md5($this->input->post('password', TRUE)));
        if ($this->form_validation->run() == FALSE) {

            $this->load->view('login');
        } elseif ($result === FALSE) {
            $data1['errors'] = 'Имя пользователя или пароль неверны';
            $data['body'] = $this->load->view('login', $data1, TRUE);
            $data['title'] = 'Вход на сайт';
            $this->load->view('main', $data);
        } elseif($result === TRUE) {

            redirect('/', 'location', 301);
        }
    }

    function logout() {
        $this->session->set_userdata('logged_in',FALSE);
        //redirect('/', 'location', 301);
    }

    function token_check($token) {
        if ($token != $this->session->userdata('token')) {
            $this->form_validation->set_message('token', 'Ошибка безопасности! Перезагрузите страницу пожалуйста');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function edit() {
        echo 'edit user settings page';
    }

    function submit_edit() {
        //@TODO написать редактирование пользователя
        redirect('user/edit#notify=edited_succsess', 'location', 301);
    }

    function info($uid) {
        return $this->getuserpage($uid);
    }

    private function getuserpage($uid = NULL) {
        $this->load->model('User_model');
        $id = ($uid) ? $uid : $this->session->userdata('uid');
        $result = $this->User_model->getUser($id);
    }

    function subscribe() {
        redirect('/#notify=subscribed', 'location', 301);
    }

    function notifications() {
        echo 'notification page';
    }

    function unsubscribe() {
        redirect('user/notifications/#notify=unsubscribed', 'location', 301);
    }

}

?>
